function songHTML(sng) {
    song = parseSong(sng);
    var div = new Array();
    for (line in song) {
        for (word in song[line]) {
            div.push('<div class = "sec">');
            if (song[line][word].chord) {
                div.push('<span class = "chord">');
                div.push(song[line][word].chord);
                div.push('</span>');
            }
            div.push('<p class = "word">');
            var w = song[line][word].word.replace(" ", "&nbsp;");
            div.push(w);
            div.push('</p></div>');
        }
        div.push('<br>');
    }
    return div.join('');
}

function parseSong(song) {
    lines = song.split('\n');
    isChord = false;
    var parsed_song = new Array();
    var pared_line = new Array();
    var chr = '';
    var lyr = '';
    for (i in lines) {
        parsed_line = new Array();
        lyr = '';
        chr = '';
        for (c in lines[i]) {
            if (lines[i][c] === '<') {
                if (chr.trim() != '') {
                    parsed_line.push({
                        'chord' : chr,
                        'word' : lyr
                    });
                } else if (lyr != '') {
                    parsed_line.push({
                        'word' : lyr
                    });
                }
                chr = '';
                lyr = '';
                isChord = true;
            } else if (lines[i][c] === '>') {
                isChord = false;
            } else if (isChord) {
                chr = chr.concat(lines[i][c]);
            } else if (!isChord) {
                lyr = lyr.concat(lines[i][c]).replace(' ', '&nbsp');
            }
        }
        if (chr.trim() != '') {
            parsed_line.push({
                'word' : lyr.replace('&nbsp;'),
                'chord' : chr
            });
        } else if (lyr != '') {
            parsed_line.push({
                'word' : lyr.replace('&nbsp;'),
            });
        }
        parsed_song.push(parsed_line);
    }
    console.log(parsed_song);
    return parsed_song;
}

function parseSong1(song) {
    lines = song.split("\n");
    parsed_song = new Array();
    for (var i = 0; i < lines.length - 1; i++) {
        if (isChordLine(lines[i]) && !isEmpty(lines[i + 1])) {
            parsed_song.push(linkChordsToLyrics(lines[i], lines[i + 1]));
            i++;
        } else {
            wd = lines[i].replace(" ", "&nbsp;");
            parsed_song.push(new Array({
                "word" : wd
            }));
        }

    }
    if (i < lines.length) {
        wd = lines[i].replace(" ", "&nbsp;");
        parsed_song.push(new Array({
            "word" : wd
        }));
    }
    return parsed_song;
}

function linkChordsToLyrics(chords, Lyrics) {
    chords = findChordPositions(chords);
    parsed_line = new Array();
    if (chords[0].pos > 0) {
        lyrics = Lyrics.substring(0, chords[0].pos);
        lyrics = (lyrics) ? lyrics.replace(" ", "&nbsp") : "&nbsp;";
        parsed_line.push({
            'word' : lyrics
        });
    }
    for (var i = 0; i < chords.length - 1; i++) {
        lyrics = Lyrics.substring(chords[i].pos, chords[i + 1].pos);
        lyrics = (lyrics) ? lyrics.replace(" ", "&nbsp") : "&nbsp;";
        parsed_line.push({
            'word' : lyrics,
            'chord' : chords[i].chord
        });
    }
    //push the last chord
    lyrics = Lyrics.substring(chords[i].pos, Lyrics.length);
    lyrics = (lyrics) ? lyrics.replace(" ", "&nbsp") : "&nbsp;";
    parsed_line.push({
        'word' : lyrics,
        'chord' : chords[i].chord
    });
    return parsed_line;
}

function findChordPositions(chord_line) {
    pos = new Array();
    chord = '';
    chord_pos = 0;
    for (var i = 0; i < chord_line.length; i++) {
        if (chord_line[i] === '<') {
            chord_pos = i;
        } else if (chord_line[i] === '>') {
            pos.push({
                'pos' : chord_pos,
                'chord' : chord
            });
            chord = '';
        } else if (chord_line[i].trim() != '') {
            chord = chord + chord_line[i];
        }
    }
    console.log(pos);
    return pos;
}

function isChordLine(line) {
    var bool = false;
    ln = line.split(" ");
    if (line.trim() != '') {
        for (word in ln) {
            if (ln[word].match(/^[<*>]/)) {
                bool = true;
            } else if (ln[word].trim() === '') {
                bool = true;
            } else {
                bool = false;
                break;
            }
        }
    }
    return bool;
}

function isEmpty(line) {
    return (line.trim() === '') ? true : false;
}

/**************************************************/  
